/**
 * Copyright &copy; 2012-2016 <a href="https://istep.lczyfz.com">Istep</a> All rights reserved.
 */
package com.lczyfz.istep.modules.teach.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lczyfz.istep.common.constants.DistributeOrNot;
import com.lczyfz.istep.common.utils.JsonModel;
import com.lczyfz.istep.modules.resource.entity.*;
import com.lczyfz.istep.modules.resource.service.ResourceCoursesystemService;
import com.lczyfz.istep.modules.resource.service.ResourceCtfSetService;
import com.lczyfz.istep.modules.resource.service.ResourcePaperService;
import com.lczyfz.istep.modules.resource.service.ResourceTrainprogramService;
import com.lczyfz.istep.modules.teach.entity.*;
import com.lczyfz.istep.modules.teach.service.*;
import com.sun.org.apache.xpath.internal.operations.*;
import net.sf.json.JSON;
import org.activiti.engine.impl.util.json.JSONObject;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.lczyfz.istep.common.config.Global;
import com.lczyfz.istep.common.persistence.Page;
import com.lczyfz.istep.common.web.BaseController;
import com.lczyfz.istep.common.utils.StringUtils;

import java.io.Writer;
import java.lang.String;
import java.util.*;

/**
 * 教学管理Controller
 * @author cqh
 * @version 2017-01-12
 */
@Controller
@RequestMapping(value = "${adminPath}/teach/teachDistribute")
public class TeachDistributeController extends BaseController {

	@Autowired
	private TeachClassService teachClassService;
	@Autowired
	private TeachStudentService teachStudentService;
	@Autowired
	private ResourceTrainprogramService resourceTrainprogramService;
	@Autowired
	private TeachDistributeService teachDistributeService;
	@Autowired
	private TeachDistributeCoursesystemService teachDistributeCoursesystemService;
	@Autowired
	private TeachDistributeCtfsetService teachDistributeCtfsetService;
	@Autowired
	private TeachDistributePaperService teachDistributePaperService;
	@Autowired
	private ResourceCoursesystemService resourceCoursesystemService;
	@Autowired
	private ResourceCtfSetService resourceCtfSetService;
	@Autowired
	private ResourcePaperService resourcePaperService;
	@Autowired
	private TeachExamService teachExamService;
	@Autowired
	private TeachStudentExamService teachStudentExamService;
	
	@ModelAttribute
	public TeachDistribute get(@RequestParam(required=false) String id) {
		TeachDistribute entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = teachDistributeService.get(id);
		}
		if (entity == null){
			entity = new TeachDistribute();
		}
		return entity;
	}
	
	@RequiresPermissions("teach:teachDistribute:view")
	@RequestMapping(value = {"list", ""})
	public String list(TeachDistribute teachDistribute, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<TeachDistribute> page = teachDistributeService.findPage(new Page<TeachDistribute>(request, response), teachDistribute);
		model.addAttribute("page", page);
		return "modules/teach/teachDistributeList";
	}

	//添加下发-页面跳转
	@RequiresPermissions("teach:teachDistribute:view")
	@RequestMapping(value = "form")
	public String form(TeachDistribute teachDistribute, Model model) {
		logger.debug("==Evan==teachDistribute.getId():"+teachDistribute.getId());

		List<TeachClass> findAllClassName = teachClassService.findList(new TeachClass());
		List<TeachStudent> findAllStudentName = teachStudentService.findList(new TeachStudent());
		List<ResourceTrainprogram> findAllTrainProgramName = resourceTrainprogramService.findAllTrainProgramName();

		List<ResourceCoursesystem> findAllCourseSystemName = resourceCoursesystemService.findList(new ResourceCoursesystem());
		List<ResourceCtfSet> findAllCtfSetName = resourceCtfSetService.findList(new ResourceCtfSet());
		List<ResourcePaper> findAllPaperName = resourcePaperService.findList(new ResourcePaper());

		if (teachDistribute.getId() != null && !teachDistribute.getId().equals("")) {
			//课程体系
			TeachDistributeCoursesystem teachDistributeCoursesystem = new TeachDistributeCoursesystem();
			teachDistributeCoursesystem.setDistributeId(teachDistribute.getId());
			List<TeachDistributeCoursesystem> temp = teachDistributeCoursesystemService.findList(teachDistributeCoursesystem);
			for (ResourceCoursesystem aFindAllCourseSystemName : findAllCourseSystemName) {
				if (temp.size() == 0) {
					break;
				} else {
					for (int j = 0; j < temp.size(); j++) {
						if (aFindAllCourseSystemName.getId().equals(temp.get(j).getCoursesystemId())) {
							aFindAllCourseSystemName.setIsChoose("true");
							temp.remove(j);
							break;
						}
					}
				}
			}
			//CTF题目集
			TeachDistributeCtfset teachDistributeCtfset = new TeachDistributeCtfset();
			teachDistributeCtfset.setDistributeId(teachDistribute.getId());
			List<TeachDistributeCtfset> temp1 = teachDistributeCtfsetService.findList(teachDistributeCtfset);
			for (ResourceCtfSet aFindAllCtfSetName : findAllCtfSetName) {
				if (temp1.size() == 0) {
					break;
				} else {
					for (int j = 0; j < temp1.size(); j++) {
						if (aFindAllCtfSetName.getId().equals(temp1.get(j).getCtfsetId())) {
							aFindAllCtfSetName.setIsChoose("true");
							temp1.remove(j);
							break;
						}
					}
				}
			}
			//试卷
			TeachDistributePaper teachDistributePaper=new TeachDistributePaper();
			teachDistributePaper.setDistributeId(teachDistribute.getId());
			List<TeachDistributePaper> temp2=teachDistributePaperService.findList(teachDistributePaper);
			for (ResourcePaper aFindAllPaperName : findAllPaperName) {
				if (temp2.size() == 0) {
					break;
				} else {
					for (int j = 0; j < temp2.size(); j++) {
						if (aFindAllPaperName.getId().equals(temp2.get(j).getPaperId())) {
							aFindAllPaperName.setIsChoose("true");
							temp2.remove(j);
							break;
						}
					}
				}

			}
		}

		model.addAttribute("findAllClassName",findAllClassName);
		model.addAttribute("findAllStudentName",findAllStudentName);
		model.addAttribute("findAllTrainProgramName",findAllTrainProgramName);
		model.addAttribute("findAllCourseSystemName",findAllCourseSystemName);
		model.addAttribute("findAllCtfSetName",findAllCtfSetName);
		model.addAttribute("findAllPaperName",findAllPaperName);
		model.addAttribute("teachDistribute", teachDistribute);
		return "modules/teach/teachDistributeForm";
	}

	//添加下发-根据培训方案id联动获取课程体系、CTF题目集、试卷
	@ResponseBody
	@RequestMapping(value = "findDistributeListById")
	public JsonModel findDistributeListById(@RequestParam(required = false) String trainProgramId) {
		logger.debug("==Evan==trainProgramId:"+trainProgramId);
		JsonModel jsonModel = new JsonModel();
		Map<String,List> map = new HashMap<>();
		map.put("CourseSystemList",resourceCoursesystemService.findCourseSystemById(trainProgramId));
		map.put("CtfSetList",resourceCtfSetService.findCtfSetById(trainProgramId));
		map.put("PaperList",resourcePaperService.findPaperById(trainProgramId));
		jsonModel.setDataObj(map);
		return jsonModel;
	}

	//添加下发-确认选择，根据CourseSystemId获得CourseSystemListName
	@ResponseBody
	@RequestMapping(value = "getCourseSystemNameById")
	public List<Map<String, String>> getCourseSystemNameById(@RequestParam(required = false) String CourseSystemId){
		logger.debug("==Evan==CourseSystemId: "+ CourseSystemId);
		List<Map<String, String>> CourseSystemListName = new ArrayList<>();
		if(CourseSystemId==null || CourseSystemId.equals("")){
			CourseSystemListName = null;
		}else {
			String[] CourseSystemListId = CourseSystemId.split(",");
			for (String aCourseSystemListId : CourseSystemListId) {
				Map<String, String> CourseSystemMap = new HashMap<>();
				CourseSystemMap.put("id",aCourseSystemListId);
				CourseSystemMap.put("name",resourceCoursesystemService.get(aCourseSystemListId).getCoursesystemName());
				CourseSystemListName.add(CourseSystemMap);
			}
		}
		logger.debug("==Evan==CourseSystemListName: "+ CourseSystemListName);
		return CourseSystemListName;

	}

	//添加下发-确认选择，根据CtfSetId获得CtfSetListName
	@ResponseBody
	@RequestMapping(value = "getCtfSetListNameById")
	public List<Map<String, String>> getCtfSetListNameById(@RequestParam(required = false) String CtfSetId){
		logger.debug("==Evan==CtfSetId: "+ CtfSetId);
		List<Map<String, String>> CtfSetListName = new ArrayList<>();
		if(CtfSetId==null || CtfSetId.equals("")){
			CtfSetListName = null;
		}else{
			String[] CtfSetListId = CtfSetId.split(",");
			for (String aCtfSetListId : CtfSetListId) {
				Map<String, String> CtfSetMap = new HashMap<>();
				CtfSetMap.put("id",aCtfSetListId);
				CtfSetMap.put("name",resourceCtfSetService.get(aCtfSetListId).getCtfSetName());
				CtfSetListName.add(CtfSetMap);
			}
		}
		return CtfSetListName;
	}

	//添加下发-确认选择，根据PaperId获得PaperListName
	@ResponseBody
	@RequestMapping(value = "getPaperListNameById")
	public List<Map<String, String>> getPaperListNameById(@RequestParam(required = false) String PaperId){
		logger.debug("==Evan==PaperId: "+ PaperId);
		List<Map<String, String>> PaperListName = new ArrayList<>();
		if(PaperId==null || PaperId.equals("")){
			PaperListName = null;
		}else{
			String[] PaperListId = PaperId.split(",");
			for(String aPaperListId : PaperListId){
				Map<String, String> PaperMap = new HashMap<>();
				PaperMap.put("id",aPaperListId);
				PaperMap.put("name",resourcePaperService.get(aPaperListId).getPaperName());
				PaperListName.add(PaperMap);
			}
		}
		return PaperListName;
	}

	//添加下发-添加
	//TODO 5.27之前
	//TODO 该方法暂时弃用
	@RequiresPermissions("teach:teachDistribute:edit")
	@RequestMapping(value = "save")
	public String save(TeachDistribute teachDistribute, Model model, RedirectAttributes redirectAttributes, HttpServletRequest request, HttpServletResponse response,
					   String distributeStudent, String distributeClass, String CourseSystemString, String CtfSetString, String solutionIsAllow) {
		logger.debug("==Evan==solutionIsAllow: "+solutionIsAllow);
		String[] CourseSystemArray = null;
		if(CourseSystemString != null){
			//TODO 获取下发课程体系id
			CourseSystemArray = CourseSystemString.split(",");
		}
		String[] CtfSetArray = null;
		if(CtfSetString != null){
			//TODO 获取下发CTF题目集id
			CtfSetArray = CtfSetString.split(",");
		}
		if(distributeStudent != null){
			//TODO 获取下发学生id
			String[] distributeStudentArray = distributeStudent.split(",");
//			teachDistributeService.saveDistribute(teachDistribute, null, distributeStudentArray, CourseSystemArray, CtfSetArray);
		}
		if(distributeClass != null){
			//TODO 获取下发班级id
			String[] distributeClassArray = distributeClass.split(",");
//			teachDistributeService.saveDistribute(teachDistribute, distributeClassArray, null, CourseSystemArray, CtfSetArray);
		}

//		return "";
//		if (!beanValidator(model, teachDistribute)){
//			return form(teachDistribute, model);
//		}
//		teachDistributeService.save(teachDistribute);
		addMessage(redirectAttributes, "添加下发成功");
		return "redirect:"+Global.getAdminPath()+"/teach/teachDistribute/?repage";
	}

	@RequiresPermissions("teach:teachDistribute:edit")
	@RequestMapping(value = "delete")
	public String delete(TeachDistribute teachDistribute, RedirectAttributes redirectAttributes) {
		teachDistributeService.deleteDistribute(teachDistribute);
		addMessage(redirectAttributes, "删除下发成功");
		return "redirect:"+Global.getAdminPath()+"/teach/teachDistribute/?repage";
	}


	//添加下发-下一步页面跳转，展示已选择的下发内容列表
	@RequestMapping(value = "addDistributeList")
	public String addDistributeList(TeachDistribute teachDistribute, String distributeStudent, String distributeClass,
									String CourseSystemList, String CtfSetList,String PaperList, Model model ){
		logger.debug("=Evan==CtfSetList=="+CtfSetList);

		List<ResourceCoursesystem> CourseSystem = new ArrayList<>();
		String[] CourseSystemListId = null;
		if(CourseSystemList==null || CourseSystemList.equals("")){
			CourseSystem = null;
		}else {
			CourseSystemListId = CourseSystemList.split(",");
			for (String aCourseSystemListId : CourseSystemListId) {
				CourseSystem.add(resourceCoursesystemService.get(aCourseSystemListId));
			}
		}

		List<ResourceCtfSet> CtfSet = new ArrayList<>();
		String[] CtfSetListId = null;
		if(CtfSetList==null || CtfSetList.equals("")){
			CtfSet = null;
		}else{
			CtfSetListId = CtfSetList.split(",");
			for (String aCtfSetListId : CtfSetListId) {
				CtfSet.add(resourceCtfSetService.get(aCtfSetListId));
			}
		}

		List<ResourcePaper> Paper = new ArrayList<>();
		String[] PaperListId = null;
		if(PaperList==null || PaperList.equals("")){
			Paper = null;
		}else{
			PaperListId = PaperList.split(",");
			for(String aPaperListId : PaperListId){
				Paper.add(resourcePaperService.get(aPaperListId));
			}
		}

		if(distributeStudent != null){
			//TODO 获取下发学生id
			String[] distributeStudentArray = distributeStudent.split(",");
			teachDistributeService.saveDistribute(teachDistribute, null, distributeStudentArray, CourseSystemListId, CtfSetListId, PaperListId);
		}
		if(distributeClass != null){
			//TODO 获取下发班级id
			String[] distributeClassArray = distributeClass.split(",");
			teachDistributeService.saveDistribute(teachDistribute, distributeClassArray, null, CourseSystemListId, CtfSetListId, PaperListId);
		}

		model.addAttribute("CourseSystem",CourseSystem);
		model.addAttribute("CtfSet",CtfSet);
		model.addAttribute("Paper",Paper);
		model.addAttribute("CourseSystemList",CourseSystemList);
		model.addAttribute("CtfSetList",CtfSetList);
		model.addAttribute("PaperList",PaperList);
		model.addAttribute("distributeStudent",distributeStudent);
		model.addAttribute("distributeClass",distributeClass);
		model.addAttribute("teachDistributeId",teachDistribute.getId());
		return "modules/teach/teachAddDistributeList";
	}

	//添加下发-下一步-配置CTF题目集
	@ResponseBody
	@RequestMapping(value = "configCtfSet")
	public String configCtfSet(String teachDistributeId, String CtfSetId, String solutionIsAllow) {
		logger.debug("=Evan==teachDistributeId: "+teachDistributeId+ "   CtfSetId: "+ CtfSetId +"   solutionIsAllow: "+ solutionIsAllow);
		TeachDistributeCtfset teachDistributeCtfset = new TeachDistributeCtfset();
		teachDistributeCtfset.setDistributeId(teachDistributeId);
		teachDistributeCtfset.setCtfsetId(CtfSetId);
		List<TeachDistributeCtfset> CtfSetSingleRecord = teachDistributeCtfsetService.findList(teachDistributeCtfset);
		CtfSetSingleRecord.get(0).setSolutionIsAllow(solutionIsAllow);
		teachDistributeCtfsetService.save(CtfSetSingleRecord.get(0));
		return solutionIsAllow;
	}

	//判断CTF题目集是否进行了配置
	@ResponseBody
	@RequestMapping(value = "configCtfSetOrNot")
	public JsonModel configCtfSetOrNot(String teachDistributeId, String CtfSetList) {
		logger.debug("=Evan===configCtfSetOrNot===teachDistributeId: "+teachDistributeId+ "   CtfSetList: "+ CtfSetList);
		JsonModel jsonModel = new JsonModel();
		if(CtfSetList != null && !CtfSetList.isEmpty()){
			String[] CtfSetListId = CtfSetList.split(",");
			TeachDistributeCtfset teachDistributeCtfset = new TeachDistributeCtfset();
			teachDistributeCtfset.setDistributeId(teachDistributeId);
			for (String aCtfSetListId : CtfSetListId) {
				teachDistributeCtfset.setCtfsetId(aCtfSetListId);
				List<TeachDistributeCtfset> CtfSetSingleRecord = teachDistributeCtfsetService.findList(teachDistributeCtfset);
				if (CtfSetSingleRecord.get(0).getSolutionIsAllow() == null || CtfSetSingleRecord.get(0).getSolutionIsAllow() .equals("")) {
					jsonModel.setMsg(resourceCtfSetService.get(aCtfSetListId).getCtfSetName());
					jsonModel.setSuccess(false);
					return jsonModel;
				}
			}
		}
		jsonModel.setSuccess(true);
		return jsonModel;
	}

	//添加下发-下一步-配置试卷页面跳转
	@RequestMapping(value = "configPaperForm")
	public String configPaperForm(String teachDistributeId, String paperId, String distributeStudent, String distributeClass,
								  String CourseSystemList, String CtfSetList,String PaperList, Model model ) {
		logger.debug("=Evan===configPaperForm===teachDistributeId: "+teachDistributeId+"  paperId: "+paperId);
		logger.debug("=Evan==configPaperForm===teachDistributeId: " + teachDistributeId + "  distributeStudent: "+distributeStudent + "  distributeClass: "+distributeClass + "  CourseSystemList: "+CourseSystemList+ "  CtfSetList: "+CtfSetList+ "  PaperList: "+PaperList);

		TeachExam getSingleRecord = teachExamService.getSingleRecord(teachDistributeId, paperId);

//		TeachExam teachExam = new TeachExam();
//		teachExam.setDistributeId(teachDistributeId);
//		teachExam.setPaperId(paperId);
//		List<TeachExam> teachExam2 = teachExamService.findList(teachExam);

//		model.addAttribute("teachExam", teachExam2.get(0));
		model.addAttribute("teachExam", getSingleRecord);
		model.addAttribute("teachDistributeId", teachDistributeId);
		model.addAttribute("paperId", paperId);
		model.addAttribute("CourseSystemList",CourseSystemList);
		model.addAttribute("CtfSetList",CtfSetList);
		model.addAttribute("PaperList",PaperList);
		model.addAttribute("distributeStudent",distributeStudent);
		model.addAttribute("distributeClass",distributeClass);
		return "modules/teach/configPaperForm";
	}

	//添加下发-下一步-配置试卷
	@RequestMapping(value = "configPaper")
	public String configPaper(TeachExam teachExam, String teachDistributeId, String paperId,  String distributeStudent, String distributeClass,
							  String CourseSystemList, String CtfSetList,String PaperList, Model model, RedirectAttributes redirectAttributes) {
		logger.debug("=Evan===configPaper===teachExam: "+teachExam);
		logger.debug("=Evan===configPaper===teachDistributeId: "+teachDistributeId+"  paperId: "+paperId+"  distributeStudent: "+distributeStudent);

		String[] StudentListId = distributeStudent.split(",");
		//判断该次下发的对象中所有学生是否已经下发过该试卷
//		for (String aStudentListId1 : StudentListId) {
//			if (teachExamService.getSingleRecordCheckExam(paperId, aStudentListId1).size() == 0) {
//				//插入考试表
//				teachExam.setDistributeId(teachDistributeId);
//				teachExam.setPaperId(paperId);
//				teachExamService.save(teachExam);
//			}
//		}

		//插入考试表
		teachExam.setDistributeId(teachDistributeId);
		teachExam.setPaperId(paperId);
		teachExamService.save(teachExam);

		//插入学生-考试中间表
		for (String aStudentListId : StudentListId) {
			TeachStudentExam teachStudentExam = new TeachStudentExam();
			teachStudentExam.setDistributeId(teachDistributeId);
			teachStudentExam.setExamId(teachExam.getId());
			teachStudentExam.setExamFrequency(teachExam.getExamFrequency());
			teachStudentExam.setUserId(aStudentListId);
			teachStudentExamService.save(teachStudentExam);
		}

		//为了返回到展示已选择的下发内容列表
		List<ResourceCoursesystem> CourseSystem = new ArrayList<>();
		String[] CourseSystemListId;
		if(CourseSystemList==null || CourseSystemList.equals("")){
			CourseSystem = null;
		}else {
			CourseSystemListId = CourseSystemList.split(",");
			for (String aCourseSystemListId : CourseSystemListId) {
				CourseSystem.add(resourceCoursesystemService.get(aCourseSystemListId));
			}
		}

		List<ResourceCtfSet> CtfSet = new ArrayList<>();
		String[] CtfSetListId;
		if(CtfSetList==null || CtfSetList.equals("")){
			CtfSet = null;
		}else{
			CtfSetListId = CtfSetList.split(",");
			for (String aCtfSetListId : CtfSetListId) {
				CtfSet.add(resourceCtfSetService.get(aCtfSetListId));
			}
		}

		List<ResourcePaper> Paper = new ArrayList<>();
		String[] PaperListId;
		if(PaperList==null || PaperList.equals("")){
			Paper = null;
		}else{
			PaperListId = PaperList.split(",");
			for(String aPaperListId : PaperListId){
				Paper.add(resourcePaperService.get(aPaperListId));
			}
		}

		model.addAttribute("teachDistributeId", teachDistributeId);
		model.addAttribute("CourseSystemList",CourseSystemList);
		model.addAttribute("CtfSetList",CtfSetList);
		model.addAttribute("PaperList",PaperList);
		model.addAttribute("distributeStudent",distributeStudent);
		model.addAttribute("distributeClass",distributeClass);
		model.addAttribute("CourseSystem",CourseSystem);
		model.addAttribute("CtfSet",CtfSet);
		model.addAttribute("Paper",Paper);
		addMessage(redirectAttributes, "成功配置试卷！");
		return "modules/teach/teachAddDistributeList";
	}

	//判断试卷是否进行了配置
	@ResponseBody
	@RequestMapping(value = "configPaperOrNot")
	public JsonModel configPaperOrNot(String teachDistributeId, String PaperList) {
		logger.debug("=Evan===configPaperOrNot===teachDistributeId: "+teachDistributeId+ "   PaperList: "+ PaperList);
		JsonModel jsonModel = new JsonModel();
		if(PaperList != null && !PaperList.isEmpty()){
			String[] PaperListId = PaperList.split(",");

//			TeachExam teachExam = new TeachExam();
//			teachExam.setDistributeId(teachDistributeId);
//
//			for (String aPaperListId : PaperListId) {
//				teachExam.setPaperId(aPaperListId);
//				List<TeachExam> PaperSingleRecord = teachExamService.findList(teachExam);
//				if (PaperSingleRecord.get(0).getExamName() == null || PaperSingleRecord.get(0).getExamName().equals("")) {
//					jsonModel.setMsg(resourcePaperService.get(aPaperListId).getPaperName());
//					jsonModel.setSuccess(false);
//					return jsonModel;
//				}
//			}

			for (String aPaperListId : PaperListId) {
				TeachExam getSingleRecord = teachExamService.getSingleRecord(teachDistributeId, aPaperListId);
				if (getSingleRecord == null ) {
					jsonModel.setMsg(resourcePaperService.get(aPaperListId).getPaperName());
					jsonModel.setSuccess(false);
					return jsonModel;
				}
			}
		}
		jsonModel.setSuccess(true);
		return jsonModel;
	}

	//添加-确认下发
	@RequestMapping(value = "confirmDistribute")
	public String confirmDistribute(String teachDistributeId, String distributeStudent, String distributeClass, String CourseSystemList, String CtfSetList, String PaperList, RedirectAttributes redirectAttributes) {
		logger.debug("=Evan==confirmDistribute===teachDistributeId: " + teachDistributeId + "  distributeStudent: "+distributeStudent + "  distributeClass: "+distributeClass + "  CourseSystemList: "+CourseSystemList+ "  CtfSetList: "+CtfSetList+ "  PaperList: "+PaperList);
		//TODO 将下发表的是否下发标志位设为 1
		TeachDistribute teachDistribute = teachDistributeService.getSingleRecord(teachDistributeId);
		teachDistribute.setDistributeOrNot(DistributeOrNot.distributeYes);
		teachDistributeService.save(teachDistribute);

		List<String> CourseSystemList2 = new LinkedList<>();
		if(CourseSystemList != null && !CourseSystemList.isEmpty()){
			String[] CourseSystemArray = CourseSystemList.split(",");
			Collections.addAll(CourseSystemList2, CourseSystemArray);
		}

		List<String> CtfSetList2 = new LinkedList<>();
		if(CtfSetList != null && !CtfSetList.isEmpty()){
			String[] CtfSetArray = CtfSetList.split(",");
			Collections.addAll(CtfSetList2, CtfSetArray);
		}

		List<String> PaperList2 = new LinkedList<>();
		if(PaperList != null && !PaperList.isEmpty()){
			String[] PaperArray = PaperList.split(",");
			Collections.addAll(PaperList2, PaperArray);
		}

		//TODO 下发对象是：学生
		if(distributeStudent != null && !distributeStudent.equals("") && !distributeStudent.isEmpty()){
			logger.debug("=================object==distributeStudent===================");
			String[] distributeStudentArray = distributeStudent.split(",");
			List<String> distributeStudentList2 = new LinkedList<>();
			Collections.addAll(distributeStudentList2, distributeStudentArray);
			//TODO 插入课程计数表
			teachDistributeService.saveAndDuplicateCourse(teachDistribute, distributeStudentList2, CourseSystemList2);
			//TODO 插入CTF题目集计数表
			teachDistributeService.saveAndDuplicateCtfSet(teachDistribute, distributeStudentList2, CtfSetList2);
			//TODO 插入试卷计数表
			teachDistributeService.saveAndDuplicatePaper(teachDistribute, distributeStudentList2, PaperList2);
		}

		//TODO 下发对象是：班级
		if(distributeClass != null && !distributeClass.equals("") && !distributeClass.isEmpty()){
			logger.debug("=================object==distributeClass===================");
			String[] distributeClassArray = distributeClass.split(",");
			for (String aDistributeClassArray : distributeClassArray) {
				List<String> distributeStudentList = new LinkedList<>();
				for (int j = 0; j < teachStudentService.findStudentByClassId(aDistributeClassArray).size(); j++) {
					distributeStudentList.add(teachStudentService.findStudentByClassId(aDistributeClassArray).get(j).getId());
				}
				logger.debug("aDistributeClassArray:"+aDistributeClassArray+"  distributeStudentList:"+distributeStudentList);
				//TODO 插入课程计数表
				teachDistributeService.saveAndDuplicateCourse(teachDistribute, distributeStudentList, CourseSystemList2);
				//TODO 插入CTF题目集计数表
				teachDistributeService.saveAndDuplicateCtfSet(teachDistribute, distributeStudentList, CtfSetList2);
				//TODO 插入试卷计数表
				teachDistributeService.saveAndDuplicatePaper(teachDistribute, distributeStudentList, PaperList2);
			}
		}
		addMessage(redirectAttributes, "添加下发成功！");
		return "redirect:"+Global.getAdminPath()+"/teach/teachDistribute/?repage";
	}

	//查看下发课程体系
	@RequiresPermissions("teach:teachDistribute:view")
	@ResponseBody
	@RequestMapping(value = "viewCourseSystem")
	public List<TeachDistribute> viewCourseSystem(@RequestParam(required = false) String ids) {
		return teachDistributeService.viewCourseSystem(ids);
	}

	//查看下发CTF题目集
	@RequiresPermissions("teach:teachDistribute:view")
	@ResponseBody
	@RequestMapping(value = "viewCTFSet")
	public List<TeachDistribute> viewCTFSet(@RequestParam(required = false) String ids) {
		return teachDistributeService.viewCTFSet(ids);
	}

	//查看下发试卷
	@RequiresPermissions("teach:teachDistribute:view")
	@ResponseBody
	@RequestMapping(value = "viewPaperName")
	public List<TeachDistribute> viewPaperName(@RequestParam(required = false) String ids) {
		return teachDistributeService.viewPaperName(ids);
	}

	//查看下发对象：学生或班级
	@RequiresPermissions("teach:teachDistribute:view")
	@ResponseBody
	@RequestMapping(value = "viewTargetName")
	public List<TeachDistribute> viewTargetName(@RequestParam(required = false) String ids) {
		return teachDistributeService.viewTargetName(ids);
	}


}